package com.itmuch.cloud.study.task;

import com.itmuch.cloud.study.service.EmailOptionService;
import com.itmuch.cloud.study.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 齐丹枫 on 2017/3/27 16:39.
 */

@Component
public class scheduledTaskToSendEmail {

    @Autowired
    private EmailOptionService emailOptionService;


    @Scheduled(cron = "0 0 17 * * ?")
    public void reportCurrentTime() {

        ExcelUtils poi = new ExcelUtils();

        String path = System.getProperty("user.dir") + System.getProperty("file.separator");
        List<List<String>> list = poi.read(path + "receiver.xlsx");
        Map<String, List<String>> maps = new HashMap<String, List<String>>();
        if (list != null) {
            for (int i = 1; i < list.size(); i++) {
                String plantCode = list.get(i).get(1).substring(0, list.get(i).get(1).length() - 2);
                String mail = list.get(i).get(4);
                if (maps.containsKey(plantCode)) {
                    maps.get(plantCode).add(mail);
                } else {
                    List<String> mails = new ArrayList<String>();
                    mails.add(mail);
                    maps.put(plantCode, mails);
                }
            }

        }

        emailOptionService.sendEmail("sinotrans@jcmos.com",maps,"防串货系统扫码结单状态表。<br> 请下载附件查看订单详细信息，<br> 谢谢！<br> 以上邮件为系统自动发送","防串货系统订单详情");
    }


    @Scheduled(cron = "0 0 12 ? * SUN")
    public void delEmailLog(){
        emailOptionService.delEmailLog();
    }
}
